{layout name="public/layout" /}
{include file='yxnavbar'}


<div class="main">
    {include file='../../plugin/coupon/template/web/common_nav'}
    <div class="panel panel-default alert_form" role="tabpanel" >
        <div class="panel-heading demo" >
            <span>优惠券添加</span>
        </div>
        <div class="panel-body" style="z-index:188;display: flex;flex-direction: column;" >
            <form action="" method="post" class="form-horizontal" role="form" name="form" id="form2" style="padding:10px;">
                <input type="hidden" name="themeid" value="{$themeid}"/>
                <div class="form-group  ad_mq" style="display: block;">
                    <label class="col-xs-8 col-sm-4 col-md-3 col-lg-2 control-label">优惠券名称</label>
                    <div class="col-sm-8 col-xs-12">
                        <div class="input-group" style="width: 300px;">
                            <input  type="text"  class="form-control"  placeholder="请输入优惠券名称" name="name" value="{$items['name']}">
                        </div>
                    </div>
                </div>
                {if $_C['coupon_use_type']!=3}
                <div class="form-group  ad_mq" style="display: block;">
                    <label class="col-xs-8 col-sm-4 col-md-3 col-lg-2 control-label">优惠券金额</label>
                    <div class="col-sm-8 col-xs-12">
                        <div class="input-group" style="width: 460px;">
                            <span class="input-group-addon">最小</span>
                            <input  type="text"  class="form-control"  placeholder="请输入最小金额" name="price" value="{$items['price']}">
                            <span class="input-group-addon">元，最大</span>
                            <input  type="text"  class="form-control"  placeholder="请输入最大金额" name="max_price" value="{$items['max_price']}">
                            <span class="input-group-addon">元</span>
                        </div>
                        <span class="help-block" style="color: red;">100元内减免金额支持小数点后1位，100元以上减免金额仅支持整数</span>
                    </div>

                </div>
                {else}
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">阶梯优惠：</label>
                    <div class="col-sm-10 col-xs-12">
                        <div class="yx_coupon_items">
                            <div class="diy_coupon_prices">
                                <span class="input-group-addon diy_coupon_span">满</span>
                                <input name="yx_lt_coupon_price_limit[]"  type="text" class="form-control span3 diy_coupon_input"
                                value="{$row['limit']}"/>
                                <span class="input-group-addon diy_coupon_span">元，减免</span>
                                <input name="yx_coupon_rule_price_diff[]"  type="text" class="form-control span3 diy_coupon_input"
                                value="{$row['price']}"/>
                                <span class="input-group-addon diy_coupon_span">元</span>
                                <input  name="yx_coupon_price_id[]" type="hidden" value="{$row['id']}" />
                            </div>
                        </div>
                        <a href="javascript:;" onclick="addLtRule();"><i class="glyphicon glyphicon-plus" title="添加优惠"></i>添加优惠</a>
                        <span class="help-block" style="color: red;">100元内减免金额支持小数点后1位，100元以上减免金额仅支持整数</span>
                    </div>
                </div>
                {/if}
                <div class="form-group  ad_mq" style="display: block;">
                    <label class="col-xs-8 col-sm-4 col-md-3 col-lg-2 control-label">优惠券库存</label>
                    <div class="col-sm-8 col-xs-12">
                        <div class="input-group" style="width: 300px;">
                            <input  type="number"  class="form-control"  placeholder="请输入优惠券库存" name="qty" value="10000">
                        </div>
                    </div>
                </div>

                {if $is_presale}
                <!--                <div class="form-group">-->
                <!--                    <label class="col-sm-2 col-xs-12 control-label">领取身份</label>-->
                <!--                    <div class="col-sm-5 col-xs-12">-->
                <!--                        <select class="form-control" style="width: 140px;" name="target" id="target">-->
                <!--                            {loop $select_target_arr $item}-->
                <!--                            <option value="{$item['value']}" {if $items['target']==$item['value']}selected{/if} >{$item['title']}</option>-->
                <!--                            {/loop}-->
                <!--                        </select>-->
                <!--                    </div>-->
                <!--                </div>-->
                <div class="form-group">
                    <label class="col-sm-2 col-xs-12 control-label">领取方式</label>
                    <div class="col-sm-5 col-xs-12">
                        <select class="form-control" style="width: 140px;" name="get_type" id="get_type">
                            {loop $select_get_type_arr $item}
                            <option value="{$item['value']}" {if $items['get_type']==$item['value']}selected{/if}>{$item['title']}</option>
                            {/loop}
                        </select>
                    </div>
                </div>
                {/if}

                <div class="form-group">
                    <label class="col-sm-2 col-xs-12 control-label">使用时间</label>
                    <div class="col-sm-4 col-xs-12">
                        <select class="form-control" style="width: 140px;" name="show_type" id="show_type">
                            {loop $select_show_type_arr $item}
                            <option value="{$item['value']}" {if $items['show_type']==$item['value']}selected{/if}>{$item['title']}</option>
                            {/loop}
                        </select>
                    </div>
                </div>
                <div class="form-group"  id="used_time" {if !$items['show_type'] || $items['show_type']==1}style="display: none;"{/if}>
                <label class="col-sm-2 col-xs-12 control-label">使用时间设置</label>
                <div class="col-sm-4 col-xs-12">
                    {php echo view_form_field_daterange('usedtime', array('start'=>$items['used_start_time'],'end'=>$items['used_end_time']), true);}
                </div>
        </div>
        {if $is_presale}
        <div class="form-group">
            <label class="col-sm-2 col-xs-12 control-label">使用条件</label>
            <div class="col-sm-5 col-xs-12">
                <select class="form-control" style="width: 140px;" name="used_type" id="used_type">
                    {loop $select_used_type_arr $item}
                    <option value="{$item['value']}" {if $items['used_type']==$item['value']}selected{/if}>{$item['title']}</option>
                    {/loop}
                </select>
            </div>
        </div>
        {/if}



        <div class="form-group">
            <label class="col-xs-12 col-sm-4 col-md-3 col-lg-2 control-label"><span style="color: red;">*</span>状态</label>
            <div class="col-sm-8 col-xs-12" style="padding-top: 6px;">
                <input  name="status" value="1" checked type="radio"> 启用
                &nbsp;&nbsp;
                <input  name="status" value="2"  type="radio"> 禁用
            </div>
        </div>
        <div class="form-group" id="sub_upload1" >
            <div class="col-sm-12">
                <input name="submit" onclick="addOrEditItem()"  type="button" value="提交" class="btn btn-primary col-lg-2" style="float: left;    width: 200px;">
            </div>
        </div>
        </form>
    </div>
</div>
</div>

<style>
    .diy_coupon_prices{
        display: flex;
    }
    .diy_coupon_span{
        width: 100px;
    }
    .diy_coupon_input{
        width: 120px;
    }
</style>
<script type="text/javascript">
    var max_id = "{php echo $max_id;}";
    $('#show_type').change(function () {
        var val = $("#show_type").val();
        if (val==2){
            $("#used_time").show();
        }else{
            $("#used_time").hide();
        }
    });
    //新增奖品 提交数据
    function addOrEditItem() {
        let postUrl = "{php echo createWebUrlPlugin('coupon.edit',array('themeid'=>$themeid));}";
        let success_redirect = "{php echo createWebUrlPlugin('coupon',array('themeid'=>$themeid));}";
        let formData = $("#form2").serialize();
        $.ajax({
            url:postUrl,
            dataType:'json',
            method:'post',
            data:formData,
            success:function (e) {
                let status = e.status;
                let result = e.result;
                if (status!=1){
                    layer.msg(result.msg)
                }else{
                    layer.msg(result.msg);
                    window.location.href = success_redirect;
                }
            },
            fail:function (err) {
                console.log(err)
            }
        })
    }
    window.addEventListener("keydown", function(e) {
        //可以判断是不是mac，如果是mac,ctrl变为花键
        //event.preventDefault() 方法阻止元素发生默认的行为。
        if((e.key=='s'||e.key=='S')&&(navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)){
            e.preventDefault();
            addOrEditItem();
        }
    }, false);
    function addLtRule() {
        var html = '<div class="diy_coupon_prices">\n' +
            '                                <span class="input-group-addon diy_coupon_span">满</span>\n' +
            '                                <input name="yx_lt_coupon_price_limit[]" type="text" class="form-control span3 diy_coupon_input"\n' +
            '                                       value=""/>\n' +
            '                                <span class="input-group-addon diy_coupon_span">元，减免</span>\n' +
            '                                <input name="yx_coupon_rule_price_diff[]" type="text" class="form-control span3 diy_coupon_input"\n' +
            '                                       value=""/>\n' +
            '                                <span class="input-group-addon diy_coupon_span">元</span>\n' +
            '                                <input  name="yx_coupon_price_id[]" type="hidden" value="'+max_id+'" />\n' +
            '                                <a href=\'javascript:;\' onclick=\'removeLtRuleItem(this)\'>\n' +
            '                                    <i class=\'glyphicon glyphicon-remove\'></i>\n' +
            '                                </a>\n' +
            '                            </div>';
        $(".yx_coupon_items").append(html);
        max_id = parseInt(max_id)+1;
    }
    function removeLtRuleItem(obj) {
        $(obj).parent().remove();
    }
</script>
